package com.hbut.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        CustomException customException = null;
        if (e instanceof CustomException) {
            customException = (CustomException) e;
        }else {
            String errorMsg = e.getMessage();
            customException = new CustomException(errorMsg);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errorMsg", customException.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
